package com.i_art_engineer.meigong.photo.text;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.i_art_engineer.meigong.R;
import com.i_art_engineer.meigong.photo.filter.FilterTools;
import com.i_art_engineer.meigong.photo.filter.GPUImage;
import com.i_art_engineer.meigong.photo.filter.GPUImageFilter;
import com.i_art_engineer.meigong.photo.filter.GPUImageView;

/**
 * Created by shf on 2016/5/26.
 */
public class MyImageView2 extends LinearLayout{
    private ImageView iv;
    private TextView tv;
   private  String mstring;
    private int filterType=0;
    private GPUImageView mFilterView;
    private Context mContext;


    public Bitmap getmBitmap() {
        return mBitmap;
    }

    public void setmBitmap(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
    }

    private  Bitmap mBitmap;
    private  LinearLayout linearLayout;
    private LinearLayout xuanzhong;
    public MyImageView2(Context context,String str,int index) {
        super(context);
        mstring=str;
        filterType=index;
        mContext=context;
        LayoutInflater.from(context).inflate(R.layout.image_item, this,
                true);
        linearLayout=(LinearLayout)findViewById(R.id.ll_container);
        xuanzhong= (LinearLayout) findViewById(R.id.xuanzhong);
        iv = (ImageView) findViewById(R.id.iv);
        tv = (TextView) findViewById(R.id.tv);
        tv.setText(str);
    }

    public MyImageView2(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
    public void setDefaultImageResource(int resId) {
        iv.setImageResource(resId);
    }
   public void setDefaultImageBitmap(Bitmap bm)
   {
        mBitmap=bm;
//        doFilter(mBitmap);
       iv.setImageBitmap(mBitmap);
   }
    Bitmap mp=null;
    public void doFilter( Bitmap bgBmp)
    {

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT
        );
        mFilterView = new GPUImageView(mContext);
        mFilterView.getGPUImage().setScaleType(GPUImage.ScaleType.CENTER_INSIDE);
        mFilterView.getGPUImage().setBackgroundColor(255,255,255);
        mFilterView.setLayoutParams(layoutParams);
        mFilterView.setImage(bgBmp);
        int w=bgBmp.getWidth();
        int h=bgBmp.getHeight();
        linearLayout.addView(mFilterView);
        GPUImageFilter filter = FilterTools.createFilterForType(mContext, FilterTools.getFilterList().filters.get(filterType));
        // 执行滤镜效果
        {
            mFilterView.setFilter(filter);
        }
        mFilterView.requestRender();


    }
    public void setDefaultTextViewText(String text) {
        tv.setText(text);
    }

    /**
     * @param resId
     */
    public void setImageResource(int resId) {
        iv.setImageResource(resId);
    }

    /**
     * @param text
     */
    public void setTextViewText(String text) {
        tv.setText(text);
    }

    /**
     * @param color
     */
    public void setTextColor(int color) {
        tv.setTextColor(color);
    }
    public void showXuanzhong(boolean b){
        if(b){
            xuanzhong.setBackgroundColor(getResources().getColor(R.color.orange));
        }else {
            xuanzhong.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        }
    }


}
